﻿namespace WMS6.Web.Inventory
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.InventoryMgtServiceRef;
    using WMS6.Web.UserControls;

    public class InvTransList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F5022;
        protected GridView gvTrans;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlHead Head1;
        protected Label lblInvTrans;
        protected Label lblMsg;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2TransList(InvTransQueryEntity invTransQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(invTransQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = InventoryMgtServiceClientProxy.GetInstance().GetInfoByInvTransQueryEntity(invTransQuery);
            base.BindData2GridView(this.gvTrans, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            InvTransQueryEntity invTransQuery = this.GetInvTransQueryEntity();
            invTransQuery.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = invTransQuery.CurrentPage;
            invTransQuery.CurrentPage = "0";
            this.gvTrans.AllowSorting = false;
            this.BindData2TransList(invTransQuery);
            this.hdnActionID.Value = "";
            this.gvTrans.ShowFooter = false;
            invTransQuery.IsGetAll = false;
            invTransQuery.CurrentPage = currentPageIndex;
            this.SetTransQueryEntity(invTransQuery);
            base.Export2Excel("invTransQuery.xls", this.gvTrans);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            InvTransQueryEntity invTransQuery = this.GetInvTransQueryEntity();
            invTransQuery.CurrentPage = "0";
            this.BindData2TransList(invTransQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            InvTransQueryEntity invTransQuery = this.GetInvTransQueryEntity();
            invTransQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2TransList(invTransQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            InvTransQueryEntity invTransQuery = this.GetInvTransQueryEntity();
            invTransQuery.CreatedBy = "";
            invTransQuery.CreatedDate = "";
            invTransQuery.TransType = "";
            invTransQuery.SourceTransType = "";
            invTransQuery.DocID = "";
            invTransQuery.OwnerID = "";
            invTransQuery.SkuID = "";
            invTransQuery.Lot = "";
            invTransQuery.FromLoc = "";
            invTransQuery.ToLoc = "";
            invTransQuery.FromLpnID = "";
            invTransQuery.ToLpnID = "";
            invTransQuery.LotAttr01 = "";
            invTransQuery.LotAttr02 = "";
            invTransQuery.LotAttr03 = "";
            invTransQuery.LotAttr04 = "";
            invTransQuery.LotAttr05 = "";
            invTransQuery.LotAttr06 = "";
            invTransQuery.LotAttr07 = "";
            invTransQuery.LotAttr08 = "";
            invTransQuery.LotAttr09 = "";
            invTransQuery.LotAttr10 = "";
            invTransQuery.ExternalLot = "";
            invTransQuery.ReceiptDate = "";
            invTransQuery.ProduceDate = "";
            invTransQuery.ExpiryDate = "";
            invTransQuery.VendorID = "";
            this.BindData2TransList(invTransQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            InvTransQueryEntity invTransQuery = this.GetInvTransQueryEntity();
            if (int.Parse(invTransQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                invTransQuery.CurrentPage = (int.Parse(invTransQuery.CurrentPage) + 1).ToString();
                this.BindData2TransList(invTransQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            InvTransQueryEntity invTransQuery = this.GetInvTransQueryEntity();
            if (int.Parse(invTransQuery.CurrentPage) > 0)
            {
                invTransQuery.CurrentPage = (int.Parse(invTransQuery.CurrentPage) - 1).ToString();
                this.BindData2TransList(invTransQuery);
            }
        }

        private InvTransQueryEntity GetInvTransQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as InvTransQueryEntity);
            }
            return (base.PageQueryEntitySession as InvTransQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                InvTransQueryEntity invTransQuery = this.GetInvTransQueryEntity();
                invTransQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2TransList(invTransQuery);
            }
        }

        protected void gvTrans_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F5022", this.gvTrans);
        }

        protected void gvTrans_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                }
            }
        }

        protected void gvTrans_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                InvTransQueryEntity invTransQueryEntity = this.GetInvTransQueryEntity();
                invTransQueryEntity.SortField = e.SortExpression;
                if (invTransQueryEntity.SortDirection == "ASC")
                {
                    invTransQueryEntity.SortDirection = "DESC";
                }
                else if (invTransQueryEntity.SortDirection == "DESC")
                {
                    invTransQueryEntity.SortDirection = "ASC";
                }
                this.BindData2TransList(invTransQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            InvTransQueryEntity invTransQuery = new InvTransQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    invTransQuery.CreatedBy = this.PreviousPage.CreatedBy;
                    invTransQuery.CreatedDate = this.PreviousPage.CreatedDate;
                    invTransQuery.TransType = this.PreviousPage.TransType;
                    invTransQuery.SourceTransType = this.PreviousPage.SourceTransType;
                    invTransQuery.DocID = this.PreviousPage.DocID;
                    invTransQuery.OwnerID = this.PreviousPage.OwnerID;
                    invTransQuery.SkuID = this.PreviousPage.SkuID;
                    invTransQuery.Lot = this.PreviousPage.Lot;
                    invTransQuery.FromLoc = this.PreviousPage.FromLoc;
                    invTransQuery.ToLoc = this.PreviousPage.ToLoc;
                    invTransQuery.FromLpnID = this.PreviousPage.FromLpn;
                    invTransQuery.ToLpnID = this.PreviousPage.ToLpn;
                    invTransQuery.LotAttr01 = this.PreviousPage.LotAttribute01;
                    invTransQuery.LotAttr02 = this.PreviousPage.LotAttribute02;
                    invTransQuery.LotAttr03 = this.PreviousPage.LotAttribute03;
                    invTransQuery.LotAttr04 = this.PreviousPage.LotAttribute04;
                    invTransQuery.LotAttr05 = this.PreviousPage.LotAttribute05;
                    invTransQuery.LotAttr06 = this.PreviousPage.LotAttribute06;
                    invTransQuery.LotAttr07 = this.PreviousPage.LotAttribute07;
                    invTransQuery.LotAttr08 = this.PreviousPage.LotAttribute08;
                    invTransQuery.LotAttr09 = this.PreviousPage.LotAttribute09;
                    invTransQuery.LotAttr10 = this.PreviousPage.LotAttribute10;
                    invTransQuery.ExternalLot = this.PreviousPage.ExternalLot;
                    invTransQuery.ReceiptDate = this.PreviousPage.ReceiptDate;
                    invTransQuery.ProduceDate = this.PreviousPage.ProduceDate;
                    invTransQuery.ExpiryDate = this.PreviousPage.ExpiryDate;
                    invTransQuery.VendorID = this.PreviousPage.VendorID;
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            invTransQuery.IsGetAll = false;
            invTransQuery.CurrentPage = "0";
            invTransQuery.PageSize = this.gvTrans.PageSize.ToString();
            invTransQuery.SortField = "TRANS_ID";
            invTransQuery.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvTrans.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetTransQueryEntity(invTransQuery);
            this.BindData2TransList(invTransQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetTransQueryEntity(InvTransQueryEntity transQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = transQuery;
            }
            else
            {
                base.PageQueryEntitySession = transQuery;
            }
        }

        public InvTransQuery PreviousPage
        {
            get
            {
                return (InvTransQuery) base.PreviousPage;
            }
        }
    }
}

